﻿using ExpressesClass.Model;
using ExpressesClass.StoApi.Base;
using ExpressesClass.StoApi.billApply.request;
using ExpressesClass.StoApi.billApply.response;
using ExpressesClass.StoApi.billCancel.request;
using ExpressesClass.StoApi.billCancel.response;
using ExpressesClass.StoApi.billStock.request;
using ExpressesClass.StoApi.billStock.response;
using ExpressesClass.StoApi.billTrace.request;
using ExpressesClass.StoApi.billTrace.response;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ExpressesClass.IExpresses
{
    public class StoExpress : IExpress
    {
        //申通快递参数
        private static string appKey = "";
        private static string secretKey = "";
        private static string resourceCode = "";
        private static string originCode = "";
        private static string siteCode = "";
        private static string customerName = "";
        private static string sitePwd = "";

        //测试
        //private static string appKey = "";
        //private static string secretKey = "";
        //private static string resourceCode = "";
        //private static string originCode = "";
        //private static string siteCode = "";
        //private static string customerName = "";
        //private static string sitePwd = "";

        public Kddh GetMailNo(Kddh kddh)
        {
            BillApplyReq billApplyReq = new BillApplyReq();

            billApplyReq.orderNo = kddh.orderId;
            billApplyReq.orderSource = originCode;
            billApplyReq.billType = "00";
            billApplyReq.orderType = "01";

            billApplyReq.sender.name = kddh.sendMan;
            billApplyReq.sender.mobile = kddh.sendManPhone;
            billApplyReq.sender.province = kddh.sendProvince;
            billApplyReq.sender.city = kddh.sendCity;
            billApplyReq.sender.area = kddh.sendCounty;
            billApplyReq.sender.address = kddh.sendManAddress;

            billApplyReq.receiver.name = kddh.receiveMan;
            billApplyReq.receiver.mobile = kddh.receiveManPhone;
            billApplyReq.receiver.province = kddh.receiveProvince;
            billApplyReq.receiver.city = kddh.receiveCity;
            billApplyReq.receiver.area = kddh.receiveCounty;
            billApplyReq.receiver.address = kddh.receiveManAddress;

            billApplyReq.cargo.battery = "10";
            billApplyReq.cargo.goodsType = "小件";
            billApplyReq.cargo.goodsName = kddh.itemName;
            billApplyReq.cargo.goodsCount = int.Parse(kddh.itemCount);

            billApplyReq.customer.siteCode = siteCode;
            billApplyReq.customer.customerName = customerName;
            billApplyReq.customer.sitePwd = sitePwd;

            billApplyReq.remark = kddh.remark;
            billApplyReq.expressDirection = "01";
            billApplyReq.createChannel = "01";
            billApplyReq.regionType = "01";

            StoClient stoClient = new StoClient(appKey, secretKey, resourceCode);

            BillApplyRsp res = stoClient.execute<BillApplyRsp>(billApplyReq);

            if (res.success == "true")
            {
                Kddh k = new Kddh();
                k.orderId = res.data.orderNo;
                k.mailNo = res.data.waybillNo;
                k.sendMan = kddh.sendMan;
                k.sendManPhone = kddh.sendManPhone;
                k.sendManAddress = kddh.sendManAddress;
                k.sendPostcode = kddh.sendPostcode;
                k.sendProvince = kddh.sendProvince;
                k.sendCity = kddh.sendCity;
                k.sendCounty = kddh.sendCounty;
                k.sendCompany = kddh.sendCompany;
                k.receiveMan = kddh.receiveMan;
                k.receiveManPhone = kddh.receiveManPhone;
                k.receiveManAddress = kddh.receiveManAddress;
                k.receivePostcode = kddh.receivePostcode;
                k.receiveProvince = kddh.receiveProvince;
                k.receiveCity = kddh.receiveCity;
                k.receiveCounty = kddh.receiveCounty;
                k.receiveCompany = kddh.receiveCompany;
                k.sortingCode = res.data.bigWord;
                k.pkgCode = res.data.packagePlace;
                k.shipName = kddh.shipName;
                k.shipVal = kddh.shipVal;
                k.itemName = kddh.itemName;
                k.itemWeight = kddh.itemWeight;
                k.itemCount = kddh.itemCount;
                k.remark = kddh.remark;
                k.bm = kddh.bm;
                k.wzh = kddh.wzh;
                k.kdCompanys = kddh.kdCompanys;
                k.scenarios = kddh.scenarios;
                k.printNum = kddh.printNum;
                k.payMethod = kddh.payMethod;
                k.isReturnNo = kddh.isReturnNo;
                return k;
            }
            else if (res.success == "false" && !string.IsNullOrEmpty(res.errorMsg))
            {
                MessageBox.Show(res.errorMsg, "提示");
            }
            else
            {
                MessageBox.Show("获取快递电子面单失败.", "提示");
            }
            return null;
        }
        public bool CancelMailNo(Kddh kddh)
        {
            BillCancelReq billCancelReq = new BillCancelReq();

            billCancelReq.sourceOrderId = kddh.orderId;
            billCancelReq.billCode = kddh.mailNo;
            billCancelReq.orderSource = originCode;
            billCancelReq.orderType = "01";

            StoClient stoClient = new StoClient(appKey, secretKey, resourceCode);

            BillCancelRsp res = stoClient.execute<BillCancelRsp>(billCancelReq);

            if (res.success == "true")
            {
                return true;
            }
            else if (res.success == "false" && !string.IsNullOrEmpty(res.errorMsg))
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ," + res.errorMsg, "提示");
            }
            else
            {
                MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单失败,请重试.", "提示");
            }
            return false;
        }
        public void GetUseNum()
        {
            BillStockReq billStockReq = new BillStockReq();
            billStockReq.siteCode = siteCode;
            billStockReq.userCode = customerName;
            billStockReq.userType = "0";
            billStockReq.password = sitePwd;
            billStockReq.timestamp = getTimeStamp();

            StoClient stoClient = new StoClient(appKey, secretKey, resourceCode);

            BillStockRsp res = stoClient.execute<BillStockRsp>(billStockReq);

            if (res.success == "true")
            {
                MessageBox.Show("可用量: " + res.data, "提示");
            }
            else if (res.success == "false" && !string.IsNullOrEmpty(res.errorMsg))
            {
                MessageBox.Show(res.errorMsg, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
        }
        public List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            BillTraceReq billTraceReq = new BillTraceReq();
            billTraceReq.order = "asc";
            billTraceReq.waybillNoList.Add(kddh.mailNo);

            StoClient stoClient = new StoClient(appKey, secretKey, resourceCode);

            BillTraceRsp res = stoClient.execute<BillTraceRsp>(billTraceReq);

            if (res.success == "true")
            {
                List<TraceLog> log = new List<TraceLog>();

                if (res.data != null && res.data.ContainsKey(kddh.mailNo))
                {
                    foreach (CommonResultVO r in res.data[kddh.mailNo])
                    {
                        TraceLog t = new TraceLog();
                        t.acceptAddress = r.opOrgName;
                        t.acceptTime = r.opTime;
                        t.scanType = r.scanType;
                        t.remark = r.memo + (string.IsNullOrEmpty(r.nextOrgName) ? "" : ",下一站:" + r.nextOrgName) + (string.IsNullOrEmpty(r.issueName) ? "" : "问题:" + r.issueName);
                        log.Add(t);
                    }
                }

                return log;
            }
            else if (res.success == "false" && !string.IsNullOrEmpty(res.errorMsg))
            {
                MessageBox.Show(res.errorMsg, "提示");
            }
            else
            {
                MessageBox.Show("查询失败", "提示");
            }
            return null;
        }
        public string getTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return ret;
        }
    }
}
